package models;

public class Game {
	public Creator creator;
	Sys sys;
	int players;
	Winner[] winner; //= new Winner[4];
	int[] coin;
	int[] stawka_gracza;
	boolean[] fold;

	public int getRankNr(int nr, int nr_karty) {
		return creator.hand[nr].cards[nr_karty].rank;
	}
    
    public int getColorNr(int nr, int nr_karty) {
		return creator.hand[nr].cards[nr_karty].color;
	}
    
    public void takeBuyInCoins(int wpisowe) {
    	for(int i = 0; i < (players); i++){
			coin[i]=coin[i]-wpisowe;
		}
    }
    
    public void takeCoins(int kasa, int nr) {
			coin[nr]=coin[nr]-kasa;
    }
    
    public void giveCoins(int kasa, int nr) {
		coin[nr]=coin[nr]+kasa;
    }
    
    public int getCoins(int numer) {
    	return coin[numer];
    }
    
    public int pobierzPuleGracza(int numer) {
    	return stawka_gracza[numer];
    }
    
    public void ustawPuleGracza(int numer, int kasa) {
    	stawka_gracza[numer]=stawka_gracza[numer]+kasa;
    }
    
    public void ustawPierwszaPuleGracza(int wpisowe) {
    	for(int i = 0; i < (players); i++){
    		stawka_gracza[i]=stawka_gracza[i]+wpisowe;
    	}
    }
    
    public void clearFolds(){
    	for(int i = 0; i < (players); i++){
    		fold[i]=false;
    	}
    }
    
    public void setFold(int nr){
    	fold[nr]=true;
    }
    
    public boolean checkFold(int nr){
    	return fold[nr];
    }
    
    public int getaNumberOfFolds(){
    	int j=0;
    	for(int i = 0; i < (players); i++){
    		if(fold[i]==true)
    			j++;
    	}
    	return j;
    }
	
	
	public Game(int humans, int bots, int coins) {
		this.sys = new Sys();
		this.players = humans + bots;
		creator = new Creator(humans, bots);
		this.coin = new int[humans+bots];
		this.stawka_gracza = new int[humans+bots];
		this.fold = new boolean[humans+bots];
		for(int i = 0; i < (humans+bots); i++){
			coin[i]=coins;
		}
	}
	
	public int Winner() {
		int i = 0, j = 1;
		Winner pomoc;
		winner = new Winner[players];
		for (i = 0; i < players; i++) {		
			winner[i] = new Winner(sys.syst(creator.hand[i].cards), i);
		}
		//sortowanie tablicy winner po lewej mniejsze
		for (i = 0; i < players; i++) {
			for (j = i + 1; j < players; j++) {
				if (winner[i].sys > winner[j].sys) {
					pomoc = winner[i];
					winner[i] = winner[j];
					winner[j] = pomoc;
				}
			}
		}
		
		
		i = 0;
		j = 1;
		while (true) {
			if (winner[i].sys < winner[j].sys) {
				return winner[i].index;
			}
			int sprawdzenie = sys.Tie(creator.hand[winner[i].index].cards, creator.hand[winner[j].index].cards, winner[j].sys);
			if (sprawdzenie == 1)
				j++;
				else
					if (sprawdzenie == 2) {
						i = j;
						j++;
					}
					else {
						return 5; //remis
					}
			if (j == players)
				return winner[i].index;
		}
	
	}
}
